//
//  OauthViewController.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/9.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
class OauthViewController: UIViewController {
    
    let webView = UIWebView()
    
    override func loadView() {
        
        view = webView
        webView.delegate = self
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        loadOauthPage()
    }
    override func viewWillDisappear(animated: Bool) {
        
        super.viewWillDisappear(animated)
        
        SVProgressHUD.dismiss()
    }
    
    
    //加载授权页面
    func loadOauthPage (){
        
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id+"&redirect_uri=" + redirect_uri
        
        
        let url = NSURL(string: urlString)
        
        let request = NSURLRequest(URL:url!)
        
        webView.loadRequest(request)
        
        
    }
    
    //设置UI界面
    func setupUI (){
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "fillAccount")
        
    }
    //关闭的点击方法
    @objc  private func close (){
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    //自动填充的点击方法
    
    @objc  private func fillAccount(){
        
        //    let jsString = "document.getElementById('userId').value = 'leiggee@126.com' , document.getElementById('passwd').value = 'oyonomg';"
        //    webView.stringByEvaluatingJavaScriptFromString(jsString)
        let jsString = "document.getElementById('userId').value = '15635395802',document.getElementById('passwd').value = 'zxg419'"
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
    }
    
}

//专门处理webVIew的所有协议方法
extension OauthViewController:UIWebViewDelegate{
    
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        guard let urlString = request.URL?.absoluteString else{
            
            return false
        }
        
        //如果是网页请求授权页面就true
        if urlString.hasPrefix("https://api.weibo.com/"){
            
            return true
        }
        //如果不是包含www.itcast.cn的就都false
        if !urlString.hasPrefix(redirect_uri){
            
            return false
        }
        
        //到了这里就是包含www.itcast.cn的网址
        
        guard let query = request.URL?.query else{
            
            return false
        }
        
        let codeString = "code="
        
        let code = query.substringFromIndex(codeString.endIndex)
        
        UserAccountViewModel().loadAccessToken(code) { (error) -> () in
            
            
            if error != nil {
                
                SVProgressHUD.showInfoWithStatus("网络君正在睡觉,请稍后再来")
                
                return
            }
            
            self.dismissViewControllerAnimated(true, completion: nil)
            
            NSNotificationCenter.defaultCenter().postNotificationName("tiaozhuan", object: "welcome")
            
        }
        return false
    }
    
    
}

